<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        v-loading="formLoading"
    >
      <el-form-item label="所属设施" prop="facilityId">
        <FacilitySelector v-model="formData.facilityId" />
      </el-form-item>
<!--      <el-form-item label="设备标识" prop="deviceId">-->
<!--        <el-input v-model="formData.deviceId" placeholder="请输入设备标识" />-->
<!--      </el-form-item>-->
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="属性关键字" prop="attrKey">
        <el-input v-model="formData.attrKey" placeholder="请输入属性关键字" />
      </el-form-item>

<!--      <el-form-item label="类型" prop="type">-->
<!--        <el-select v-model="formData.type" placeholder="请选择类型">-->
<!--          <el-option-->
<!--              v-for="dict in getStrDictOptions(DICT_TYPE.DEVICE_ATTR_TYPE)"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="数据类型" prop="dataType">
        <el-select v-model="formData.dataType" placeholder="请选择数据类型">
          <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.DEVICE_ATTR_DATA_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="说明" prop="description">
        <el-input v-model="formData.description" type="textarea" placeholder="请输入说明" />
      </el-form-item>
      <el-form-item label="当前值" prop="value">
        <el-input v-model="formData.value" placeholder="请输入当前值" />
      </el-form-item>
<!--      <el-form-item label="是否可控" prop="controllable">-->
<!--        <el-radio-group v-model="formData.controllable">-->
<!--          <el-radio-->
<!--              v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"-->
<!--              :key="dict.value"-->
<!--              :label="dict.value"-->
<!--          >-->
<!--            {{ dict.label }}-->
<!--          </el-radio>-->
<!--        </el-radio-group>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否采集" prop="collectable">-->
<!--        <el-radio-group v-model="formData.collectable">-->
<!--          <el-radio-->
<!--              v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"-->
<!--              :key="dict.value"-->
<!--              :label="dict.value"-->
<!--          >-->
<!--            {{ dict.label }}-->
<!--          </el-radio>-->
<!--        </el-radio-group>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="分组" prop="attrGroup">-->
<!--        <el-input v-model="formData.attrGroup" placeholder="请输入分组" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="读写方式" prop="readWriteMode">-->
<!--        <el-select v-model="formData.readWriteMode" placeholder="请选择读写方式">-->
<!--          <el-option-->
<!--              v-for="dict in getStrDictOptions(DICT_TYPE.DEVICE_ATTR_READ_MODE)"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="初始值" prop="initialValue">-->
<!--        <el-input v-model="formData.initialValue" placeholder="请输入初始值" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="控制变量名" prop="controlVariable">-->
<!--        <el-input v-model="formData.controlVariable" placeholder="请输入控制变量名" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="采集变量名" prop="collectVariable">-->
<!--        <el-input v-model="formData.collectVariable" placeholder="请输入采集变量名" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="允许最大值" prop="maxValue">-->
<!--        <el-input v-model="formData.maxValue" placeholder="请输入允许最大值" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="允许最小值" prop="minValue">-->
<!--        <el-input v-model="formData.minValue" placeholder="请输入允许最小值" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="展示格式化" prop="displayFormat">-->
<!--        <el-input v-model="formData.displayFormat" type="textarea" placeholder="请输入展示格式化" />-->
<!--      </el-form-item>-->
      <el-form-item label="参数MAP" prop="settingMap">
        <el-input v-model="formData.settingMap" type="textarea" placeholder="请输入参数MAP" :autosize="{ minRows: 4 }" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import * as DeviceAttrApi from '@/api/yz/deviceattr'
import {useRoute} from "vue-router";
import FacilitySelector from "@/views/yz/facility/FacilitySelector.vue";

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  deviceId: undefined,
  attrKey: undefined,
  name: undefined,
  type: undefined,
  dataType: undefined,
  description: undefined,
  value: undefined,
  controllable: undefined,
  collectable: undefined,
  attrGroup: undefined,
  readWriteMode: undefined,
  initialValue: undefined,
  controlVariable: undefined,
  collectVariable: undefined,
  maxValue: undefined,
  minValue: undefined,
  displayFormat: undefined,
  facilityId: undefined,
  settingMap: undefined,
})
const formRules = reactive({
  deviceId: [{ required: true, message: '设备标识不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  setDefaultFacilityId()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DeviceAttrApi.getDeviceAttr(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DeviceAttrApi.DeviceAttrVO
    if (formType.value === 'create') {
      await DeviceAttrApi.createDeviceAttr(data)
      message.success(t('common.createSuccess'))
    } else {
      await DeviceAttrApi.updateDeviceAttr(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    deviceId: undefined,
    attrKey: undefined,
    name: undefined,
    type: undefined,
    dataType: undefined,
    description: undefined,
    value: undefined,
    controllable: undefined,
    collectable: undefined,
    attrGroup: undefined,
    readWriteMode: undefined,
    initialValue: undefined,
    controlVariable: undefined,
    collectVariable: undefined,
    maxValue: undefined,
    minValue: undefined,
    displayFormat: undefined,
    facilityId: undefined,
    settingMap: undefined,
  }
  formRef.value?.resetFields()
}
const route = useRoute()
const setDefaultFacilityId = () => {
  formData.value.facilityId = Number.parseInt(route.query.facilityId)
}
</script>
